/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/24 15:41
 */
package org.mechanic.fund.service.product.impl;

import org.mechanic.core.Constants;
import org.mechanic.fund.domain.product.ProductStocksIndexType;
import org.mechanic.fund.domain.utils.mapper.DtoBeanMapper;
import org.mechanic.fund.repository.product.ProductStocksIndexTypeRepository;
import org.mechanic.fund.service.product.IProductStocksIndexTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 基准信息类型服务实现
 *
 * @author mechanic
 * @version 0.0.1
 */
@Service
public class ProductStocksIndexTypeService implements IProductStocksIndexTypeService {
    private static Logger logger = LoggerFactory.getLogger(ProductStocksIndexTypeService.class);
    private ProductStocksIndexTypeRepository stocksIndexTypeRepository;

    @Autowired
    public ProductStocksIndexTypeService(ProductStocksIndexTypeRepository stocksIndexTypeRepository) {
        this.stocksIndexTypeRepository = stocksIndexTypeRepository;
    }

    /**
     * 返回按sort字段升序排序的基准信息类型列表
     *
     * @return 基准信息类型列表
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public List<ProductStocksIndexType> getProductStocksIndexTypeList() {
        Sort.Order order = new Sort.Order(Direction.ASC, "sort");
        Sort sort = new Sort(order);
        return (List<ProductStocksIndexType>) this.stocksIndexTypeRepository.findAll(sort);
    }

    /**
     * 返回匹配指定主键编号的对象
     *
     * @param id 主键编号
     * @return 对象实体
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public ProductStocksIndexType getById(String id) {
        return this.stocksIndexTypeRepository.findOne(id);
    }

    /**
     * 保存。 主键为空则create，不为空则update。
     *
     * @param productStocksIndexTypes 被保存的对象
     */
    @Override
    public void save(List<ProductStocksIndexType> productStocksIndexTypes, String operationUserId) {
        this.stocksIndexTypeRepository.save(DtoBeanMapper.setOperationToList(productStocksIndexTypes, operationUserId));
    }

    /**
     * 删除匹配主键ID的对象
     *
     * @param id 主键ID
     */
    @Override
    public void delete(String id) {
        try {
            this.stocksIndexTypeRepository.delete(id);
        } catch (EmptyResultDataAccessException e) {
            logger.info(id + "不存在");
        }
    }

}
